package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.hotel.saas.core.dao.PresaleCouponCategoryJointDAO;
import com.ecoolex.hotel.saas.entity.PresaleCouponCategoryJoint;
import com.ecoolex.hotel.saas.entity.PresaleCouponCategoryJoint.Column;
import com.ecoolex.hotel.saas.entity.PresaleCouponCategoryJointExample;
import com.ecoolex.hotel.saas.entity.PresaleCouponCategoryJointExample.Criteria;
import com.ecoolex.hotel.saas.pojo.AddPresaleCouponCategoryJointDTO;

import java.sql.Date;
import java.time.Instant;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PresaleCouponCategoryJointManager {
	
    @Autowired
    private PresaleCouponCategoryJointDAO dao;
    
    public Long presaleCouponJointCount(Integer categoryId) {
    	PresaleCouponCategoryJointExample example = new PresaleCouponCategoryJointExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andCategoryIdEqualTo(categoryId).andDeletedEqualTo(PresaleCouponCategoryJoint.NOT_DELETED);
		return dao.countByExample(example);
	}

	public PresaleCouponCategoryJoint get(Integer id) {
		return dao.selectByPrimaryKeyWithLogicalDelete(id, PresaleCouponCategoryJoint.NOT_DELETED);
	}

	public Pagination<PresaleCouponCategoryJoint> query(Integer categoryId, String presaleCouponName, Page page) {
		return Pagination.create(page, 
			dao.count(categoryId, presaleCouponName), 
			() -> dao.query(categoryId, presaleCouponName, page));
	}

	public Boolean add(List<AddPresaleCouponCategoryJointDTO> addList, String createUser){
		addList.forEach(add ->{
			add.setDeleted(PresaleCouponCategoryJoint.NOT_DELETED);
			add.setCreateUser(createUser);
			add.setCreateTime(Date.from(Instant.now()));
			add.setUpdateUser(createUser);
			add.setUpdateTime(Date.from(Instant.now()));
			dao.insert(add);
		});
		return true;
	}

	public PresaleCouponCategoryJoint edit(PresaleCouponCategoryJoint edit, String updateUser){
		edit.setUpdateUser(updateUser);
		edit.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(edit, Column.updateUser,Column.updateTime,Column.weight);
		return edit;
	}

	public Boolean del(PresaleCouponCategoryJoint edit, String updateUser){
		edit.setDeleted(PresaleCouponCategoryJoint.IS_DELETED);
		edit.setUpdateUser(updateUser);
		edit.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(edit, Column.deleted,Column.updateUser,Column.updateTime);
		return true;
	}
    
    public void deleteByCategoryId(Integer categoryId, String updateUser) {
    	List<PresaleCouponCategoryJoint> list = queryByCategoryId(categoryId);
    	list.forEach(p -> {
    		p.setDeleted(PresaleCouponCategoryJoint.IS_DELETED);
    		p.setUpdateUser(updateUser);
    		p.setUpdateTime(Date.from(Instant.now()));
    		dao.updateByPrimaryKeySelective(p, Column.deleted,Column.updateUser,Column.updateTime);
    	});
    }
    
    public List<PresaleCouponCategoryJoint> queryByCategoryId(Integer categoryId){
    	PresaleCouponCategoryJointExample example = new PresaleCouponCategoryJointExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andCategoryIdEqualTo(categoryId).andDeletedEqualTo(PresaleCouponCategoryJoint.NOT_DELETED);
    	return dao.selectByExample(example);
    }
    
    public Boolean isRelationCategory(Integer preCouponId, Integer categoryId) {
    	PresaleCouponCategoryJointExample example = new PresaleCouponCategoryJointExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andCategoryIdEqualTo(categoryId).andPreCouponIdEqualTo(preCouponId).andDeletedEqualTo(PresaleCouponCategoryJoint.NOT_DELETED);
		return dao.countByExample(example) > 0;
	}
}